package com.spirng.leetcode.day01;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArrayTest6_2 {
    public static void main(String[] args) {
        int [][] array= new int [][]{{2,3,4},{5,6,7},{8,9,10},{11,12,13},{14,15,16}};
        ArrayTest6_2.spiralOrder2(array);
    }
    //代码随想录方法
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        int i = 0,j=0;
        int startx=0;
        int starty=0;
        int x= matrix.length;
        int y= matrix[0].length;
        int offset=1;
        int numEle=x*y;
        if(x==1){
            for (int c=0;c<y;c++){
                list.add(matrix[0][c]);
            }
            return list;
        }if(y==1){
            for ( int v=0;v<x;v++){
                list.add(matrix[v][0]);
            }
            return list;
        }
        while (numEle>=1){
            for (j=starty;j<y-offset;j++){
                list.add(matrix[startx][j]);
                numEle--;
            }
            for (i=startx;i<x-offset;i++){
                list.add(matrix[i][j]);
                numEle--;

            }
            for (;j>starty;j--){
                if(numEle==0)
                    break;
                list.add(matrix[i][j]);
                numEle--;

            }
            for (;i>startx;i--){
                if(numEle==0)
                    break;
                list.add(matrix[i][starty]);
                numEle--;
            }
            starty++;
            startx++;
            offset++;
//            if (list.size()==x*y){
//                break;
//            }
            if(list.size()==(x*y-1)&&x==y&&x%2==1){
                list.add(matrix[x/2][x/2]);
                numEle--;
            }
        }
        return list;
    }
    //他人题解
    private static List<Integer> spiralOrder1(int[][] matrix) {
        LinkedList<Integer> result = new LinkedList<>();
        if(matrix==null||matrix.length==0) return result;
        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int bottom = matrix.length - 1;
        int numEle = matrix.length * matrix[0].length;
        while (numEle >= 1) {
            for (int i = left; i <= right && numEle >= 1; i++) {
                result.add(matrix[top][i]);
                numEle--;
            }
            top++;
            for (int i = top; i <= bottom && numEle >= 1; i++) {
                result.add(matrix[i][right]);
                numEle--;
            }
            right--;
            for (int i = right; i >= left && numEle >= 1; i--) {
                result.add(matrix[bottom][i]);
                numEle--;
            }
            bottom--;
            for (int i = bottom; i >= top && numEle >= 1; i--) {
                result.add(matrix[i][left]);
                numEle--;
            }
            left++;
        }
        return result;
    }
    private static List<Integer> spiralOrder2(int[][] matrix) {
        LinkedList<Integer> list = new LinkedList<>();
        int left=0;
        int right=matrix[0].length-1;
        int top=0;
        int bottom=matrix.length-1;
        int nums=matrix[0].length*matrix.length;
        while (nums>=1){
            for (int i=left;i<=right&&nums!=0;i++){
                list.add(matrix[top][i]);
                nums--;
            }
            top++;
            for (int i=top;i<=bottom&&nums!=0;i++){
                list.add(matrix[i][right]);
                nums--;

            }
            right--;
            for (int i=right;i>=left&&nums!=0;i--){
                list.add(matrix[bottom][i]);
                nums--;

            }
            bottom--;
            for (int i=bottom;i>=top&&nums!=0;i--){
                list.add(matrix[i][left]);
                nums--;
            }
            left++;
        }
        return list;
    }
}
